<template>
  <div class="pushRecord">
    <ZgNewPage
      title="推送任务管理"
      ref="page"
      :extraHeight="365"
      :scrollX="1500"
      :pageOpt="{}"
      :batchBtns="batchBtns"
      :dataBtns="dataBtns"
      :conditions="conditions"
      @search="getList"
      @batch="onBatch"
      @admin="toAdmin"
    >
      <template slot="columns">
        <a-table-column
          v-for="(h, i) in headers"
          :key="`column${i}`"
          :width="h.width === 0 ? 'auto' : h.width"
          align="center"
          :title="h.label"
        >
          <template slot-scope="text, record">
            <template v-if="h.code === 'status'">
              <a-tag color="green" v-if="text === 'success'">成功</a-tag>
              <a-tag color="blue" v-else-if="text === 'running'">运行中</a-tag>
              <a-tag color="cyan" v-else-if="text === 'partial'">部分成功</a-tag>
              <a-tag color="red" v-else>失败 </a-tag>
            </template>
            <template v-else-if="h.code === 'pushMode'">
              <span>{{ text === 'full' ? '全量' : '增量' }}</span>
            </template>
            <template v-else-if="h.code === 'pushMethod'">
              <span>{{ text === 'database' ? '数据库' : '接口' }}</span>
            </template>
            <template v-else>
              {{ record[h.code] || '--' }}
            </template>
          </template>
        </a-table-column>
      </template>
    </ZgNewPage>
    <ZgDialog v-model="dialogVisible" @ok="save" :title="dialogTitle">
      <Detail :id="detailId" ref="detail" />
      <div class="wfull" slot="footer"><a-button type="primary" @click="dialogVisible = false">确定</a-button></div>
    </ZgDialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      batchBtns: [
        // { key: 1, label: '批量删除' },
        // { key: 2, label: '批量修改' },
        // { key: 3, label: '数据排序' }
      ],
      dataBtns: [
        {
          key: 'detail',
          name: '详情',
          type: 'link',
          isShow: () => true
        },
        {
          key: 'manualRePush',
          name: '手动重推',
          type: 'link',
          isShow: () => true
        }
      ],
      conditions: [{ code: 'taskName', compType: '单行文本', label: '任务名称', defaultVal: '' }],
      headers: [
        { code: 'taskName', label: '任务名称', isShow: true },
        { code: 'businessKey', label: '业务主键', isShow: true },
        { code: 'status', label: '执行状态', isShow: true },
        { code: 'pushMethod', label: '推送方式', isShow: true },
        { code: 'pushMode', label: '推送模式', isShow: true },
        { code: 'errorMessage', label: '错误信息', isShow: true },
        { code: 'startTime', label: '开始时间', isShow: true },
        { code: 'endTime', label: '结束时间', isShow: true }
      ],
      dialogVisible: false,
      dialogTitle: '查看推送记录详情',
      detailId: undefined,
      // 网关前缀
      gateWayPrefix: '/zg-datapush'
    }
  },
  components: {
    Detail: () => import('./detail.vue')
  },
  computed: {},
  methods: {
    // 获取列表
    getList(params) {
      this.$http.post(`${this.gateWayPrefix}/datapush/record/page`, params, (data) => {
        const { records, total } = data
        this.$refs.page.endSearch({
          total: Number(total),
          list: records
        })
      })
    },
    toAdmin(data) {
      const { key, obj } = data
      this.detailId = obj.id
      if (key === 'detail') {
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.detail.getRecordDetail()
        })
      } else {
        this.confirm(() => {
          this.$http.post(`${this.gateWayPrefix}/datapush/record/rePush`, { recordId: this.detailId }, (data) => {
            this.$message.success('重推任务已启动')
            this.$refs.page.search()
          })
        }, '确定要重推这条数据么')
      }
    },
    save() {
      this.dialogVisible = false
    },
    onBatch() {}
  },
  created() {},
  mounted() {}
}
</script>
<style lang="less" scoped></style>
